#include "st.h"
#include "ui_st.h"
#include<QSqlQuery>

ST::ST(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ST)
{
    ui->setupUi(this);
}

ST::~ST()
{
    delete ui;
}

void ST::on_return_2_clicked()
{
    this->close();
}

//GPA算法函数
double getGPA1(int x){
    if(x>=90)return 4;
    else if(x>=85&&x<90)return 3.7;
    else if(x>=82&&x<85)return 3.3;
    else if(x>=78&&x<82)return 3;
    else if(x>=75&&x<78)return 2.7;
    else if(x>=72&&x<75)return 2.3;
    else if(x>=68&&x<72)return 2;
    else if(x>=64&&x<68)return 1.5;
    else if(x>=60&&x<64)return 1;
    else return 0;
}

//查看成绩按钮
void ST::on_pushButton_2_clicked()
{
    st_look_inf=new ST_look_inf(this);
    st_look_inf->setWindowTitle("个人成绩");
    st_look_inf->show();

    QSqlQuery query;
    QString id=ui->label_4->text();

    query.prepare("select cpp,shudian,lisan,gaoshu,xiandai,dawu from gpa where id=:id");
    query.bindValue(":id",id);
    query.exec();

    int cpp,shudian,lisan,gaoshu,xiandai,dawu;
    double gpa;
    query.next();
    cpp=query.value(0).toInt();
    shudian=query.value(1).toInt();
    lisan=query.value(2).toInt();
    gaoshu=query.value(3).toInt();
    xiandai=query.value(4).toInt();
    dawu=query.value(5).toInt();

    gpa=(getGPA1(cpp)*3+getGPA1(shudian)*3+getGPA1(lisan)*3\
         +getGPA1(gaoshu)*5+getGPA1(xiandai)*3+getGPA1(dawu)*4)/21;
    QString s_gpa=QString::number(gpa);

    st_look_inf->ui->tip->setText("GPA:"+s_gpa);
    st_look_inf->ui->label->setText("C++ : "+query.value(0).toString());
    st_look_inf->ui->label_2->setText("数字电路 : "+query.value(1).toString());
    st_look_inf->ui->label_3->setText("离散数学 : "+query.value(2).toString());
    st_look_inf->ui->label_4->setText("高等数学 : "+query.value(3).toString());
    st_look_inf->ui->label_5->setText("线性代数 : "+query.value(4).toString());
    st_look_inf->ui->label_6->setText("大学物理 : "+query.value(5).toString());

}

//修改个人信息按钮
void ST::on_pushButton_3_clicked()
{
    set_st=new SET_st(this);
    set_st->setWindowTitle("修改个人信息");
    set_st->show();

    set_st->ui->comboBox->setCurrentText(ui->label_5->text());
    set_st->ui->comboBox_2->setCurrentText(ui->label_6->text());

    QString id=ui->label_4->text();

    set_st->ui->label->setText(ui->tip->text());
    set_st->ui->id->setText(id);

    QSqlQuery query;
    query.prepare("select password from st where id=:id");
    query.bindValue(":id",id);
    query.exec();
    if(query.next()){
       set_st->ui->password->setText(query.value(0).toString());
    }

}
